home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dbase / lib19.zip / STATS.PRG < prev    next >
Text File  |  1992-06-25  |  26KB  |  595 lines

  1. *-------------------------------------------------------------------------------
  2. *-- Program...: STATS.PRG
  3. *-- Programmer: Ken Mayer (KENMAYER) and Jay Parsons (JPARSONS)
  4. *-- Date......: 06/25/1992
  5. *-- Notes.....: Statistical Functions -- see README.TXT to include this 
  6. *--             library file in your system.
  7. *-------------------------------------------------------------------------------
  8.  
  9. FUNCTION Samplevar
  10. *-------------------------------------------------------------------------------
  11. *-- Programmer..: Jay Parsons (Jparsons)
  12. *-- Date........: 4/13/1992
  13. *-- Notes.......: Finds sample variance of specified field of the current
  14. *--             : database, using CALCULATE command.
  15. *--             : The CALCULATE command calculates the population variance,
  16. *--             : which is smaller by a factor of (n-1)/n.
  17. *--             :
  18. *-- Written for.: dBASE IV Version 1.5
  19. *-- Rev. History: Original function 1990.
  20. *--             : Modified to take optional parameter, 4/13/1992
  21. *-- Calls       : None
  22. *-- Called by...: Any
  23. *-- Usage.......: Samplevar( <cField> [, <cClause> ] )
  24. *-- Example.....: ? Samplevar( "Balance", ".FOR..NOT. isblank( Balance )" )
  25. *-- Returns     : a numeric or float value, the sample variance, or .F. if
  26. *--             : it cannot be calculated.
  27. *--             : If any of the numeric items are floats, the result will be.
  28. *-- Parameters..: cField, name of a numeric field of the current database
  29. *--             : for which to calculate the sample variance
  30. *--             : cClause, optional, a FOR, WHILE, TO, etc. clause
  31. *-------------------------------------------------------------------------------
  32.    PARAMETERS cField, cCondition
  33.    PRIVATE fVar, nCount, cCond
  34.    IF pcount() = 2
  35.       cCond = " "+ cCondition
  36.    ELSE
  37.       cCond = ""
  38.    ENDIF
  39.    CALCULATE VAR( &cField ), CNT() TO fVar, nCount &cCond
  40.  
  41. RETURN iif( nCount > 1, fVar * nCount / ( nCount - 1 ), .F. )
  42. *-- Eof: Samplevar()
  43.  
  44. FUNCTION Stny
  45. *-------------------------------------------------------------------------------
  46. *-- Programmer..: Jay Parsons (Jparsons)
  47. *-- Date........: 11/13/1990
  48. *-- Notes.......: Returns value of the standard normal distribution function
  49. *--             : given a number of standard deviations from the mean.
  50. *--             : This function is not useful alone.  The standard normal
  51. *--             : distribution function is the familiar bell-shaped curve
  52. *--             : scaled so its mean is at 0, each standard deviation is 1
  53. *--             : and the total area under the curve is 1.  The function
  54. *--             : Stnarea calls on this function to calculate the approximate
  55. *--             : area (a fraction equal to percent of the total) under the
  56. *--             : part of the curve lying betwen the mean and the given
  57. *--             : number of standard deviations.
  58. *--             :
  59. *-- Written for.: dBASE IV
  60. *-- Rev. History: None
  61. *-- Calls       : None
  62. *-- Called by...: Any
  63. *-- Usage.......: Stny( <nDevs> )
  64. *-- Example.....: ? Stny( 1 )
  65. *-- Returns     : numeric value of the function.
  66. *-- Parameters..: nDevs, standard deviations from the mean
  67. *-------------------------------------------------------------------------------
  68.    PARAMETERS nDevs
  69.  
  70. RETURN exp( -nDevs * nDevs / 2 ) / sqrt( 2 * pi() )
  71. *-- EoF: Stny()
  72.  
  73. FUNCTION Stnarea
  74. *-------------------------------------------------------------------------------
  75. *-- Programmer..: Jay Parsons (Jparsons)
  76. *-- Date........: 11/13/1990
  77. *-- Notes.......: Area of the standard normal distribution function between
  78. *--             : mean and given number of standard deviations from the mean.
  79. *--             :
  80. *--             : What's it about?  Well, College Board scores (originally)
  81. *--             : were based on a normal distribution with a mean of 500 and
  82. *--             : 100 points per standard deviation.  Knowing that a 650
  83. *--             : score is 1.5 standard deviations from the 500 mean, we
  84. *--             : can calculate Stnarea( 1.5 ) as .4332.  This tells us that
  85. *--             : 43.32% of the scores lie between 650 and 500.  Since 50%
  86. *--             : lie below 500, a score of 650 beats 93.32% of the scores.
  87. *--             :
  88. *--             : The polynomial approximation used by this function is said
  89. *--             : to be accurate to .00001, 1/1000 of one percent.  Remember
  90. *--             : to SET DECIMALS appropriately to view results.
  91. *--             :
  92. *-- Written for.: dBASE IV
  93. *-- Rev. History: None
  94. *-- Calls       : Stny()            Function in STATS.PRG
  95. *-- Called by...: Any
  96. *-- Usage.......: Stnarea( <nDevs> )
  97. *-- Example.....: ? Stnarea( 1.5 )
  98. *-- Returns     : % of area between deviations given and the mean, 0<=a<.5.
  99. *-- Parameters..: nDevs, standard deviations from the mean
  100. *-------------------------------------------------------------------------------
  101.    PARAMETERS nDevs
  102.    PRIVATE nX, nV
  103.    nX = abs( nDevs )
  104.    nV =  1 / ( 1 + .33267 * nX )
  105.  
  106. RETURN .5 - Stny( nX ) * ( .4361836  * nV - .1201676 * nV * nV ;
  107.      + .937298 * nV * nV * nV )
  108. *-- EoF: Stnarea()
  109.  
  110. FUNCTION Stnz
  111. *-------------------------------------------------------------------------------
  112. *-- Programmer..: Jay Parsons (Jparsons)
  113. *-- Date........: 11/13/1990
  114. *-- Notes.......: A lookup table to find the values of "z", standard
  115. *--             : deviations, corresponding to the most common areas inside a
  116. *--             : given number of tails of the normal distribution function.
  117. *--             :
  118. *--             : Used in testing confidence intervals.  If a sample of
  119. *--             : light bulbs from a shipment shows an average life of 1150
  120. *--             : hours, and the criterion for rejection of the shipment is
  121. *--             : 95% confidence that the average life of all bulbs is less
  122. *--             : than (a single tail) 1200 hours, the value 1.64485 returned
  123. *--             : by this function is necessary to determine whether to
  124. *--             : reject the shipment or not.
  125. *--             :
  126. *--             : Values of "z" that are not found in the table can be found
  127. *--             : using Stndevs, below, but it is slow.
  128. *--             :
  129. *-- Written for.: dBASE IV
  130. *-- Rev. History: None
  131. *-- Calls       : None
  132. *-- Called by...: Any
  133. *-- Usage.......: Stnz( <nProb>, <nTails> )
  134. *-- Example.....: ? Stnz( .95, 1 )
  135. *-- Returns     : z, number of standard deviations from mean inside which
  136. *--             : ( or to the side of which includes the mean, if one tail)
  137. *--             : the given percentage of area will fall.
  138. *--             : Returns -1 if no entry in table.
  139. *-- Parameters..: nConf, confidence desired, 0 < nConf < 1
  140. *--             : nTails, 1 or 2 = number of tails of curve of interest
  141. *-------------------------------------------------------------------------------
  142.    PARAMETERS nConf, nTails
  143.    IF nTails # 1 .AND. nTails # 2
  144.       RETURN -1
  145.    ENDIF
  146.    DO CASE
  147.       CASE nConf = .95
  148.          RETURN iif( nTails = 1, 1.64485, 1.96010 )
  149.       CASE nConf = .99
  150.          RETURN iif( nTails = 1, 2.32676, 2.57648 )
  151.       CASE nConf = .995
  152.          RETURN iif( nTails = 1, 2.57648, 2.80794 )
  153.       CASE nConf = .999
  154.          RETURN iif( nTails = 1, 3.09147, 3.29202 )
  155.       OTHERWISE
  156.          RETURN -1
  157.    ENDCASE
  158.  
  159. *-- EoF: Stnz()
  160.  
  161. FUNCTION Stndiff
  162. *-------------------------------------------------------------------------------
  163. *-- Programmer..: Jay Parsons (Jparsons)
  164. *-- Date........: 4/13/1992
  165. *-- Notes.......: Determines whether hypothesis that sample of a given mean
  166. *--               is different from expected mean is justified.
  167. *--
  168. *--               If nPopstd, the standard deviation of the population, is
  169. *--               not known and nSample, the sample size, is greater than
  170. *--               30, the sample standard deviation may be used for nPopstd.
  171. *--
  172. *--               This function assumes the population is large relative to
  173. *--               the sample or that the sampling is with replacement.  If
  174. *--               neither is true, the right side of the expression in the
  175. *--               later return line should